require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsDrawPondHandler = {};
local this = RedDotsDrawPondHandler;

function RedDotsDrawPondHandler.init()
    CloseEvent(Config.EventType.Fresh_Bag, this.update);
    CloseEvent(Config.EventType.WishPondSourceChange, this.update);
    CloseEvent(Config.EventType.Fresh_Currency, this.update);
    CloseEvent(Config.EventType.Fresh_Free_Pond, this.update);
    ListenEvent(Config.EventType.Fresh_Currency, this.update);
    ListenEvent(Config.EventType.WishPondSourceChange, this.update);
    ListenEvent(Config.EventType.Fresh_Bag, this.update);
    ListenEvent(Config.EventType.Fresh_Free_Pond, this.update);

    return this;
end


--参数1：卡池Id， nil为所有检查所有
--参数2：抽卡数量（单抽/10连之类） table
function RedDotsDrawPondHandler.getFlag(...)
    local flag = Config.RedDotsFlagState.None;
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.DrawPond, false) then
        -- 免费次数
        local checkList = ...
        if table.contains(checkList, 1) then
            if drawHandler.getDrawPondFreeLeftTime() <= 0 then
                return Config.RedDotsFlagState.Needful
            elseif not table.contains(checkList, 10) then
                return Config.RedDotsFlagState.None
            end
        end

        local numFlag = false;
        if drawHandler.WishSource and drawHandler.WishSource.wishRewardInfo then
            for i = 1, #drawHandler.WishSource.wishRewardInfo do
                local tempWish = drawHandler.WishSource.wishRewardInfo[i]
                if (tempWish.type == 1 or tempWish.type == 3) and tempWish.rewardCount > 0 then
                    numFlag = true;
                    break;
                end
            end
            if drawHandler.WishSource and (drawHandler.WishSource.drawBigReward == 0) then
                numFlag = true;
            end
        end
        if not numFlag then
            return flag;
        end

        -- 10连
        -- local count = ...;
        local count = 10
        flag = this.getFlagByPool(count);
    end
    return flag;
end

function RedDotsDrawPondHandler.getFlagByPool(count)
    local flag = Config.RedDotsFlagState.None;
    if count + logicMgr.HeroManager.getDrawTodayCount(EnumConst.drawType.Pond) > dataTable.getGlobalInt(616) then
        return flag;
    end
    local cost = nil;
    if count == 10 then
        cost = 10;
    elseif (count == nil or count == 1) then
        cost = 1;
    end

    if cost ~= nil then
        if logicMgr.ItemManager.getBagNumByTableID(CurrencyEnum.HopeBall) >= cost then
            flag = Config.RedDotsFlagState.Needful;
        --elseif cost == 10 then
        --    local ShopGoodID = tonumber(G.dataTable["t_global"]["104"].f_string_value)
        --    local BuyData = decodeJsonStrToTabel(G.dataTable["t_shopgoods"][tostring(ShopGoodID)].f_Price)[1]
        --    local haveMoneyNum = logicMgr.ItemManager.getBagNumByTableID(BuyData[1]);
        --    if haveMoneyNum >= cost * BuyData[2] then
        --        flag = Config.RedDotsFlagState.Needful;
        --    end
        elseif GetPlayerPrefsAfter5AMFlag("DrawPondRedDots") then
            flag = Config.RedDotsFlagState.Needful;
        end
    end
    return flag;
end

function RedDotsDrawPondHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.DrawPond);
end